package com.arong.swing.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.arong.swing.db.JDBCUtils;
import com.arong.swing.db.entity.Dept;
import com.arong.swing.db.entity.PagingInfo;
import com.arong.swing.db.entity.PagingResult;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;
public class DeptSDAO  extends BaseDAO {
	public DeptSDAO(){
		this(DB_ANRONGTEC);
	}
	
	public DeptSDAO(String dbFile){
		super(dbFile);
	}
	public DeptSDAO(JDBCUtils jdbcUtils){
		super(jdbcUtils);
	}
	public String getCreateTableSql(){
		return "CREATE TABLE IF NOT EXISTS "+
				"DEPT("+//cs端组织机构也可能是检查站所以只存id和name
				  "DEPT_ID     NUMBER(12) NOT NULL,"+
				  //"REGION_ID   NUMBER(12) NOT NULL,"+
				  "DEPT_NAME   VARCHAR2(60) NOT NULL,"+
				  "UP_DEPT_ID  NUMBER(12) NOT NULL"+
//				  "ADDRESS     VARCHAR2(128),"+
//				  "POST_CODE   NUMBER(32),"+
//				  "CONTACT_TEL VARCHAR2(32),"+
//				  "CONTACT     VARCHAR2(32),"+
//				  "FAX         VARCHAR2(32),"+
//				  "DEPT_CODE         VARCHAR2(64),"+
//				  "EMAIL       VARCHAR2(64),"+
//				  "WEB_ADDRESS VARCHAR2(128),"+
//				  "CREATE_TIME DATE NOT NULL,"+
//				  "STS             CHAR(1) NOT NULL,"+
//				  "STS_TIME        DATE NOT NULL,"+
//				  "REMARK      VARCHAR2(256)"+
				")";
		
	}
	
	
	/**
	 * 插入
	 * @param entity
	 * @return
	 * @throws SysException
	 */
	public  int insert(Dept entity) throws SysException,AppException {
		int i=0;
		try {
			//entity.setSts(Constant.STS_N);
			//entity.setStsTime(new Timestamp(System.currentTimeMillis()));
		 			i= jdbcUtils.insert(entity);
		} catch (SQLException e) {
			throw new SysException("add error", e);
		}
		return i;
	}

	/**
	 * 修改
	 * @param entity
	 * @param keyPropName
	 * @return
	 * @throws SysException
	 */
	public  int update(Dept entity) throws SysException,AppException {
		int i=0;
		try {
			i=jdbcUtils.update(entity, "deptId");
		} catch (SQLException e) {
			throw new SysException("update error",e);
		}
		return i;
	}


	/**
	 * 分页查询
	 * @param sql
	 * @param clazz
	 * @param pagingInfo
	 * @param params
	 * @return 
	 * @throws SysException
	 */
	public  PagingResult<Dept> queryList(PagingInfo pagingInfo, Dept entity)
			throws SysException,AppException {
		PagingResult<Dept> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), Dept.class,
				pagingInfo, getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList分页查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询 集合
	 * @param sql
	 * @param clazz
	 * @param params
	 * @return
	 * @throws SysException
	 */
	public   List<Dept> queryList(Dept entity) throws SysException,AppException {
	    List<Dept> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), Dept.class,
			getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList集合查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询对象
	 * @param sql
	 * @param clazz
	 * @param params
	 * @return
	 * @throws SysException
	 */
	public  Dept queryBean(Dept entity) throws SysException,AppException {
		Dept res=null;
	    try {
			res=  jdbcUtils.queryBean(getSql(entity), Dept.class,
				getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryBean对象查询 error",e);
		}
		return   res;
	}

	/**
	 * 获取查询条件
	 * @return
	 */
	private Object[] getParams(Dept dept) {
		List<Object> qryList = new ArrayList<Object>();
		if (dept.getDeptId() != null) {
			qryList.add(dept.getDeptId());
		}
		if (dept.getDeptName() != null) {
			qryList.add(dept.getDeptName());
		}
		
		return super.setQryList(qryList);
	}

	/**
	 * 获取sql
	 * @return
	 */
	private String getSql(Dept dept) {
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append(" a.DEPT_ID,a.DEPT_NAME,a.UP_DEPT_ID");
		sql.append(" from DEPT a where 1=1");
		if (dept.getDeptId() != null) {
			sql.append(" and DEPT_ID=?");
		}
		if (dept.getDeptName() != null) {
			sql.append(" and DEPT_NAME=?");
		}
		return sql.toString();
	}
    
}

